#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode* head)
    {
        unordered_set<ListNode*> hash;
        while (head)
        {
            if (hash.count(head))
            {
                return true;
            }
            hash.insert(head);
            head = head->next;
        }
        return false;

    }
};